package com.study.elasticsearchstudy.controller;

import com.alibaba.fastjson.JSON;
import com.study.elasticsearchstudy.config.ElasticSearchConfig;
import org.apache.commons.collections4.map.HashedMap;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.Map;

@RestController
public class HelloElasticSearchController {

    @Autowired
    RestHighLevelClient restHighLevelClient;

    @RequestMapping("/helloEs")
    public Object helloEs() throws IOException {
        SearchRequest searchRequest = new SearchRequest("bank");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//        MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("address", "mill");
//        searchSourceBuilder.query(matchQueryBuilder);
        searchRequest.source(searchSourceBuilder);
        searchSourceBuilder.from(0);
        searchSourceBuilder.size(2000);
        SearchResponse response = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        System.out.println(response);
        for (SearchHit hit : response.getHits().getHits()) {
            String id = hit.getId();
            System.out.println("id = " + id);
            float score = hit.getScore();
            System.out.println("score = " + score);
            Map<String, Object> source = hit.getSourceAsMap();
            System.out.println(source);
        }
        return response;
    }




}
